<template>
  <div class="Hm-upload">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httprequest"
    >
      <img v-if="value" v-errorimg :src="value" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <!-- 删除图片 -->
    <div v-if="value" class="icon-box">
      <i class="el-icon-close" @click="delPic" />
    </div>
    <!--放大预览得icon  -->
    <div v-if="value" class="zoom-box">
      <i class="el-icon-zoom-in" @click="show = true" />
    </div>
    <!-- 组件预览功能 -->
    <el-dialog :visible.sync="show" title="图片预览" width="800px">
      <div style="text-align: center">
        <img v-errorimg style="width: 100%" :src="value" alt="">
      </div>
    </el-dialog>
    <!-- 进度条 -->
    <el-progress v-if="percentageShow" :percentage="percentage" />
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
var cos = new COS({
  SecretId: 'AKIDlNkqfDUVFhsnQmqeBRQ7ZkeIfla9SUGl',
  SecretKey: 'FEvLUO1XmkRrzqQknZ5JGwMWP1LOAftr'
})
export default {
  name: 'HmUpload',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      //   imageUrl: ''
      //   弹框得显示与隐藏
      show: false,
      percentage: 100,
      percentageShow: false
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      console.log(file)
      const isJPG = file.type === 'image/jpeg ' || 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 自定义上传
    httprequest(res) {
      // 获取到图片的文件对象
      const file = res.file
      // 调用（使用）云存储方法
      // 使用上传方法
      this.percentageShow = true
      //   每次上传得进度条都要从0开始
      this.percentage = 0
      cos.putObject(
        {
          Bucket: 'dhf-80-1301625390' /* 填入您自己的存储桶，必须字段 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，例如ap-beijing，必须字段 */,
          Key: file.name /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */,
          StorageClass: 'STANDARD',
          Body: file, // 上传文件对象
          //   图片上传进度
          onProgress: (progressData) => {
            this.percentage = progressData.percent * 100
            if (progressData.percent === 1) {
              setTimeout(() => {
                this.percentageShow = false
              }, 1000)
            }
            console.log(JSON.stringify(progressData))
          }
        },
        // 图片上传成功后的回调
        (err, data) => {
          //   this.imgSrc = 'http://' + data.Location
          // 通知父组件改值
          this.$emit('input', 'http://' + data.Location)
          console.log(err || data)
        }
      )
    },
    // 删除图片
    delPic() {
      // 通知父组件更新value值
      this.$emit('input', '')
    }
  }
}
</script>

<style lang="scss" scoped>
// icon样式
.Hm-upload {
  position: relative;
  display: inline-block;
  .icon-box {
    position: absolute;
    top: 6px;
    right: 6px;
    color: #fff;
    cursor: pointer;
    opacity: 0;
  }
  .zoom-box {
    position: absolute;
    bottom: 6px;
    right: 6px;
    color: #fff;
    cursor: pointer;
    opacity: 0;
  }
  &:hover {
    .icon-box,
    .zoom-box {
      opacity: 1;
    }
  }
  //   进度条样式
  .el-progress {
    width: 100%;
    position: absolute;
    top: 50%;
    left: 6%;
    color: #d9d9d9;
    //   left: 50px;
    ::v-deep .el-progress__text {
      color: #fff !important;
    }
  }
}
.avatar-uploader ::v-deep .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
